package org.feng.resource;

import java.util.List;

import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.feng.entity.EntityPool;
import org.feng.entity.ServiceProvider;
import org.feng.entity.User;
import org.jboss.logging.Logger;

import io.quarkus.hibernate.orm.panache.PanacheQuery;
import io.quarkus.security.identity.SecurityIdentity;

@Path("/isp")
@Produces(MediaType.APPLICATION_JSON)
//@Consumes(MediaType.APPLICATION_JSON)
public class SvrProviderRs {

	 private static final Logger LOG = Logger.getLogger(SvrProviderRs.class);
	 public SvrProviderRs() {

	}

	/**
	 * 根据服务商类别获取所有服务商
	 */
	public RetMsg serverList(String type, 
			String status,
			String name, 
			String province,
			String city, 
			String county,
			Integer pageIndex, 
			Integer pageSize) {// List<ServiceProvider>
		RetMsg rm = (RetMsg) (EntityPool.getEntityObject(RetMsg.class));
		String sqlWhere = " status<>-2 and type=" + type;//把标记为删除的数据滤掉
		//LOG.info("status="+status);
		if (null!=status && !("".equals(status)))
			sqlWhere += " and status=" + status;
		if (null!=name && (!"".equals(name)))
			sqlWhere += " and name like '%" + name + "%'";
		if (null!=province && (!"".equals(province)))
			sqlWhere += " and province = '" + province + "'";
		if (null!=city && (!"".equals(city)))
			sqlWhere += " and city = '" + city + "'";
		if (null!=county && (!"".equals(county)))
			sqlWhere += " and county = '" + county + "'";
		LOG.info(sqlWhere);
		PanacheQuery<ServiceProvider> pageSp = ServiceProvider.find(sqlWhere);
		//PanacheQuery<ServiceProvider> pageSp = ServiceProvider.findAll();
		//LOG.info("pageSp.count()="+pageSp.count());
		// if (pageSp.count()>pageSize)
		if (pageIndex==null||pageIndex < 1)
			pageIndex = 1;
		if (pageSize==null|| pageSize< 1)
			pageSize = 20;
		pageSp.page(pageIndex-1, pageSize);//pageIndex 在页面从1开始,但后台从0开始
		rm.data = pageSp.list();
		rm.count = pageSp.count();
		return rm;
	}

	/**
	 * 获取所有服务商
	 */
	@Path("svr_list")
	@GET
	// @Produces(MediaType.APPLICATION_JSON)
	public RetMsg svrList(
			// @QueryParam("type")String type,
			@QueryParam("status") String status, 
			@QueryParam("name") String name,
			@QueryParam("province") String province,
			@QueryParam("city") String city,
			@QueryParam("county") String county,
			@QueryParam("page") int pageIndex, // =1&=30
			@QueryParam("limit") int pageSize) {// ServiceProvider sp
		// ServiceProvider
		// sp=(ServiceProvider)EntityPool.getEntityObject(ServiceProvider.class);
		return serverList("1", status, name,province, city, county,pageIndex, pageSize);
	}

	/**
	 * 获取所有平台商
	 */
	@Path("isp_list")
	@GET
	// @Produces(MediaType.APPLICATION_JSON)
	public RetMsg ispList(
			// @QueryParam("type")String type,
			@QueryParam("status") String status, 
			@QueryParam("name") String name, 
			@QueryParam("province") String province,
			@QueryParam("city") String city,
			@QueryParam("county") String county,
			@QueryParam("page") int pageIndex, // =1&=30
			@QueryParam("limit") int pageSize) {// ServiceProvider sp
		return serverList("3", status, name, province, city, county,pageIndex, pageSize);
	}

	/**
	 * 获取所有供应商
	 */
	@Path("prv_list")
	@GET
	// @Produces(MediaType.APPLICATION_JSON)
	public RetMsg prvList(			// @QueryParam("type")String type,
			@QueryParam("status") String status, 
			@QueryParam("name") String name, 
			@QueryParam("province") String province,
			@QueryParam("city") String city,
			@QueryParam("county") String county,
			@QueryParam("page") int pageIndex, // =1&=30
			@QueryParam("limit") int pageSize) {// ServiceProvider sp
		return serverList("2", status, name, province, city, county,pageIndex, pageSize);
	}

	// 认证对象,可以获取已经认证通过的用户信息
	@Inject
	SecurityIdentity si;

	/**
	 * 新增服务商
	 */
	//@Path("add")
	@PUT
	@Produces(MediaType.APPLICATION_JSON) // 返回类型
	@Consumes(MediaType.APPLICATION_JSON) // 提交类型
	@Transactional
	public RetMsg add(ServiceProvider sp) {
		Object us = si.getAttribute("userInfo");
		if (us != null)// 如果没有登录就不设置运营商ID
			sp.parentId = ((User) us).ispId;// 设置运营商ID
		if (sp.type>2||sp.type<1)
			sp.type=1;
		//LOG.info(sp.toString());
		sp.persist();// 提交到数据库
		sp.flush();// 更新到数据库
		//增加服务商默认管理用户
		User ue=(User) (EntityPool.getEntityObject(User.class));
		ue.id=null;
		ue.type=sp.type;
		ue.ispId=sp.parentId;
		ue.svrId=sp.id;
		ue.userName=sp.nameShort+"管理员";
		ue.tel=sp.town;
		ue.status=1;
		if (sp.type==1)
			ue.roles="服务商管理";
		else
			ue.roles="供货商管理";
		ue.persist();
		RetMsg rm=(RetMsg) (EntityPool.getEntityObject(RetMsg.class));
		rm.obj=sp;
		return rm;
	}

	/**
	 * 更新或新增服务商
	 */
	//@Path("update")
	@POST
	@Produces(MediaType.APPLICATION_JSON) // 返回类型
	@Consumes(MediaType.APPLICATION_JSON) // 提交类型
	@Transactional
	public RetMsg update(ServiceProvider sp) {
		if (!si.isAnonymous()) {
			Object us = si.getAttribute("userInfo");
			if (us != null)// 如果没有登录就不设置运营商ID
				sp.parentId = ((User) us).ispId;// 设置运营商ID
		}
		//LOG.info(sp.toString());
		//sp.type=1;
		sp.getEntityManager().merge(sp);
		//sp.update("", null)
		//sp.persist();// 提交到数据库
		sp.flush();// 更新到数据库
		RetMsg rm=(RetMsg) (EntityPool.getEntityObject(RetMsg.class));
		rm.obj=sp;
		return rm;
	}
	/**
	 *  删除服务商
	 */
	@Path("{id}")
	@DELETE
	@Produces(MediaType.APPLICATION_JSON) // 返回类型
	@Transactional
	public RetMsg delete(@PathParam("id")int id) {
		RetMsg rm=(RetMsg) (EntityPool.getEntityObject(RetMsg.class));
		rm.count= ServiceProvider.update("status=-2 where id = ?1", id);
		return rm;
	}

	/**
	 *  更新服务商状态
	 */
	@Path("status/{id}/{status}")
	@GET
	@Produces(MediaType.APPLICATION_JSON) // 返回类型
	@Transactional
	public RetMsg status(@PathParam("id")int id,@PathParam("status")byte status) {
		RetMsg rm=(RetMsg) (EntityPool.getEntityObject(RetMsg.class));
		//LOG.info("status="+status +", id="+id);
		rm.count= ServiceProvider.update("status=?1 where id = ?2",status, id);
		return rm;
	}

	/**
	 * 获取自己服务商
	 */
	@Path("getOwn")
	@GET
	// @Produces(MediaType.APPLICATION_JSON)
	public ServiceProvider getOwn() {// 
		int id=1;
		Object us = si.getAttribute("userInfo");
		if (us != null)// 如果没有登录就不设置运营商ID
			id= ((User) us).svrId;// 设置运营商ID
		
		LOG.info("getOwn id="+id);
		ServiceProvider sp=ServiceProvider.findById(id);
		return sp;
	}
	/**
	 * 判断用户手机是否重复
	 */
	@Path("unique/{sname}")
	@GET
	// @Produces(MediaType.APPLICATION_JSON)
	public RetMsg isUnique(@PathParam("sname") String sname) {//
		RetMsg rm = (RetMsg) (EntityPool.getEntityObject(RetMsg.class));
		ServiceProvider sp=ServiceProvider.find("nameShort", sname).firstResult();
		LOG.info("sname="+sname+" sp="+sp );
		if (null==sp) {
			rm.code="0";
		}else {
			rm.code="1";
		}
		return rm;
	}

}
